<!--
 * Copyright (c) 2018-2025, duneng All rights reserved.
 *
 * Author: wq (meteor85@163.com) 
 * 2018年9月17日 只修改了部分展现要素，为了能让界面能展现。待后台接口完备后，再做细节处理。目前仅为静态数据
-->

<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit" v-if="industry_btn_add">添加
      </el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" height="350" element-loading-text="正在努力加载中..." border fit highlight-current-row style="width: 99%">

      <el-table-column align="center" label="序号" width="80px">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>

      <el-table-column label="工业园名称" width="180px">
        <template slot-scope="scope">
          <span>{{scope.row.industryName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="工业园类别" width="120px">
        <template slot-scope="scope">
          <!-- <span>{{scope.row.industryType}}</span> -->
          <span>{{indusType[scope.row.industryType]}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="工业园地址" width="320px">
        <template slot-scope="scope">
          <!-- <span>{{scope.row.industryLocation }}</span> -->
          <el-cascader size="large" :options="options" v-model="scope.row.industryLocation.split(',')" :disabled="true" separator=" " @change="handleChange"></el-cascader>
        </template>
      </el-table-column>
      <el-table-column align="center" label="工业园描述" width="280px">
        <template slot-scope="scope">
          <span>{{scope.row.industryDec}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="工业园示意图" width="280px">
        <template slot-scope="scope">
          <!-- <span>{{scope.row.industryImgUrl }}</span> -->
          <img style="width:240px;height:200px;" :src='scope.row.industryImgUrl'>
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间" width="100px">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="340">
        <template slot-scope="scope">
          <el-button size="mini" type="success" v-if="industry_btn_edit" @click="viewDetail(scope.row)">详情
          </el-button>
          <el-button size="mini" type="success" v-if="industry_btn_edit" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <!-- dialogUploadVisible -->
          
          <el-button size="mini" type="danger" v-if="industry_btn_del" @click="handleDelete(scope.row)">删除
          </el-button>
          <el-button size="mini" type="primary" v-if="industry_btn_edit" @click="uploadFile(scope.row)">上传模拟数据
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="工业园名称" prop="industryName">
          <el-input v-model="form.industryName" placeholder="工业园名称"></el-input>
        </el-form-item>
        <el-form-item label="工业园类别" prop="industryType">
          <!-- <el-input v-model="form.industryType" placeholder="工业园类别"></el-input> -->
            <el-radio v-model="form.industryType" label="0" border>虚拟</el-radio>
            <el-radio v-model="form.industryType" label="1" border>真实</el-radio>
        </el-form-item>
        <el-form-item label="工业园地址" prop="industryLocation">
          
          <el-cascader size="large" :options="options" v-model="provinceCity" separator=" " @change="handleChange"></el-cascader>
          <!-- <el-input v-model="form.industryLocation" placeholder="工业园地址信息"></el-input> -->
        </el-form-item>
        <el-form-item  label="工业园描述" prop="industryDec">
          <el-input type="textarea" v-model="form.industryDec" placeholder="工业园描述" style="height:100px"></el-input>
        </el-form-item>
        <el-form-item label="工业园示意图" prop="industryImgUrl" >
          <!-- <el-input v-model="form.industryImgUrl" placeholder="工业园示意图"></el-input>  -->
          <my-upload field="file" @crop-upload-success="cropUploadSuccess" v-model="show" :width="800" :height="600" url="/admin/user/upload" :headers="headers" img-format="jpg"></my-upload>
          <img :src='form.industryImgUrl'>
          <el-button type="primary" @click="toggleShow" size="mini">选择
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="uploadTitle" :visible.sync="dialogUploadVisible">
        <el-upload
          class="upload-demo"
          action="/admin/user/upload"
          :on-change="uploadFileChange"
          :file-list="fileList3">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
    </el-dialog>

    <!--<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogDeptVisible">
      <el-tree class="filter-tree" :data="treeDeptData" :default-checked-keys="checkedKeys" check-strictly node-key="id" highlight-current ref="deptTree" @node-click="getNodeData" :props="defaultProps" :filter-node-method="filterNode" default-expand-all>
      </el-tree>
    </el-dialog>-->

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogPermissionVisible">
      <el-tree class="filter-tree" :data="treeData" :default-checked-keys="checkedKeys" check-strictly node-key="id" highlight-current :props="defaultProps" show-checkbox ref="menuTree" :filter-node-method="filterNode" default-expand-all>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updatePermession(id, industryType)">更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,

} from '@/api/bus/sensor/industry';
//上传图片引入的组件
import myUpload from "vue-image-crop-upload";
import { getToken } from "@/util/auth";
import ElFormItem from "element-ui/packages/form/src/form-item.vue";
//-----------------------
import { fetchTree } from '@/api/menu';
import { mapGetters } from 'vuex';
import waves from '@/directive/waves/index.js'; // 水波纹指令
import {provinceAndCityData } from 'element-china-area-data';//引入省市区三级联动数据

export default {
  name: 'industry_list',
  components: {
    ElFormItem,
    'my-upload': myUpload
  },
  directives: {
    waves
  },
  data() {
    return {
      options:provinceAndCityData,  //为了省市区下拉框处理数据
      provinceCity: [],//为了省市区下拉框处理数据
      treeData: [],
      treeDeptData: [],
      checkedKeys: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      indusType:['虚拟','真实'],
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      form: {
        industryName: undefined,
        industryeDec: undefined,
        industryImgUrl:undefined,
        industryLocation:undefined,
        provinceId:undefined,
        cityId:undefined
      },
      // id: undefined,
      // roleCode: undefined,
      rules: {
        industryName: [
          {
            required: true,
            message: '工业园区名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        industryType: [
          {
            required: true,
            message: '工业园区类别',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 10,
            message: '长度在 1 到10 个字符',
            trigger: 'blur'
          }
        ],
         industryLocation: [
          {
            required: true,
            message: '工业园区位置',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 位数字',
            trigger: 'blur'
          }
        ]
      },
      statusOptions: ['0', '1'],
      rolesOptions: undefined,
      dialogFormVisible: false,
      dialogDeptVisible: false,
      dialogPermissionVisible: false,
      dialogUploadVisible:false,
      uploadTitle:'',
      fileList3:[],
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建',
        //permission: '分配权限'
      },
      tableKey: 0,
      tableKey: 0,
      industry_btn_add: false,
      industry_btn_edit: false,
      industry_btn_del: false,
      //industry_btn_perm: false

      show: false,
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
    }
  },
  created() {
    this.getList()
    this.industry_btn_add = this.permissions['sys_industry_add']
    this.industry_btn_edit = this.permissions['sys_industry_edit']
    this.industry_btn_del = this.permissions['sys_industry_del']

  },
  computed: {
    ...mapGetters(['elements', 'permissions'])
  },
  mounted() {
  },
  methods: {
   
    handleChange (value) {
 
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create';
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        this.provinceCity =row.industryLocation.split(',');
        this.form = response.data
        
        this.dialogFormVisible = true
        this.dialogStatus = 'update';
      })
    },
    uploadFile(row){
      this.uploadTitle = '上传模拟数据文件';
      this.fileList3=[];
      this.dialogUploadVisible=true;
    },
    uploadFileChange(file, fileList){
      console.log(file.name);
      this.fileList3.push({
        name:file.name,
        url:''
      });
    },
    viewDetail(row){
      this.$router.push({
          path:'/bus/industry/detail',
          components:'views/bus/industry/detail',
          query:{
            'industryId':row.id,
          }
        });
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getNodeData(data) {
      this.dialogDeptVisible = false
      this.form.roleDeptId = data.id
      this.form.deptName = data.name
    },
    handleDelete(row) {
      delObj(row.id).then(response => {
        this.dialogFormVisible = false
        this.getList()
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    create(formName) {
      const set = this.$refs
       this.form.industryLocation = this.provinceCity.join(',')
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    update(formName) {

      const set = this.$refs
      this.form.industryLocation = this.provinceCity.join(',')
      this.form.provinceId = this.provinceCity[0]
      this.form.cityId = this.provinceCity[1];

      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          
          putObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.provinceCity=[]
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
   
    resetTemp() {
      this.form = {
        id: undefined,
        industryName: undefined,
        industryDesc: undefined
      },
      this.provinceCity=[]
    },
    /**
     * upload success
     *
     * [param] jsonData   服务器返回数据，已进行json转码
     * [param] field
     */
    cropUploadSuccess(jsonData, field) {
      console.log(jsonData);
      console.log(field);
      this.form.industryImgUrl = jsonData.filename;
    },
    toggleShow() {
      this.show = !this.show
    },
  }
}
</script>
<style scoped>
  .depImg{
    width: 260px;
    height:200px;
  }
</style>

